/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.field.FieldGenerator;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.method.CreateDataTypeGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.method.GetAccessorCreatorGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.method.GetModelResInfoGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.property.ModelInfoFieldGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public abstract class DataManagerImplGenerator extends ClassGenerator {

  protected ManagerGeneratorContext ParentContext;
  private String changeConverterName;

  public String getClassName() {
    return getName();
  }

  protected DataManagerImplGenerator(ManagerGeneratorContext parContext) {
    super(parContext.getProjContext().getCompilationUnitInfo());
    ParentContext = parContext;
  }

  @Override
  protected ClassGeneratorContext createClassInfoContext() {
    return new MgrImplCtx(ParentContext);
  }

  @Override
  protected void afterInitialize() {
    super.afterInitialize();
    MediateType mgrImplType = new MediateType("", getName());
    ParentContext.getProjContext().setMgrImplType(mgrImplType);
    ParentContext.getProjContext().getCefGenContext().setMgrImplType(mgrImplType);
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<TypeInfo> addParentInterfaces() {
    CefGeneratorContext cefGenContext = this.ParentContext.getProjContext().getCefGenContext();
    TypeInfo typeInfo = new TypeInfo(cefGenContext.getMgrAllInterfaceType().getClassName());
    typeInfo.setTypePackageName(cefGenContext.getMgrAllInterfaceType().getNamespace());
//    addImport(cefGenContext.getMgrAllInterfaceType().getFullName());
    return new java.util.ArrayList<TypeInfo>(java.util.Arrays.asList(new TypeInfo[]{typeInfo}));
  }

  @Override
  protected final TypeInfo setParentClass() {
    return new TypeInfo(getImplParentClassType());
  }

  protected abstract java.lang.Class getImplParentClassType();

  @Override
  protected String getName() {
    return ParentContext.getProjContext().getCefGenContext().getMetadata().getHeader().getCode()
        + NamePostfix;
  }

  protected static final String NamePostfix = "Manager";

  @Override
  protected ArrayList<ClassMethodGenerator> createMethodGenerators() {
    ArrayList<ClassMethodGenerator> methodGenerators = super.createMethodGenerators();
    if (methodGenerators == null) {
      methodGenerators = new ArrayList<>();
    }

    List<ClassMethodGenerator> methods = getMgrActionGenerators();
    if (methods != null) {
      methodGenerators.addAll(methods);
    }


    GetAccessorCreatorGenerator accCreateGen = get_getAccessorCreatorGenerator();
    if(accCreateGen != null)
      methodGenerators.add(accCreateGen);

    if (hasDataTypeGenerator()) {
      CreateDataTypeGenerator createGenerator = getDataTypeGenerator();
      if (createGenerator != null) {
        methodGenerators.add(createGenerator);
      }
    }

    GetModelResInfoGenerator modeInfoGenerator = new GetModelResInfoGenerator(ResoName, ParentContext);
    methodGenerators.add(modeInfoGenerator);
    //③其他自定义方法
    addExtendMethodGenerator(methodGenerators);

    return methodGenerators;
  }

  private final String ResoName = "modelInfo";
  protected abstract List<ClassMethodGenerator> getMgrActionGenerators();

  protected GetAccessorCreatorGenerator get_getAccessorCreatorGenerator() {
    return new GetAccessorCreatorGenerator(ParentContext);
  }

  protected void addExtendMethodGenerator(ArrayList<ClassMethodGenerator> generators) {
  }

  protected Boolean hasDataTypeGenerator() {
    return true;
  }

  protected abstract CreateDataTypeGenerator getDataTypeGenerator();

  @Override
  protected ArrayList<FieldGenerator> createFieldGenerator() {

    ArrayList<FieldGenerator> list = super.createFieldGenerator();
    if(list == null)
      list = new ArrayList<>();

    list.add(new ModelInfoFieldGenerator(ResoName, ParentContext));

    return list;
  }
}
